#Responsability:
#Prepare all the previous needs for the model execution

ModelUtils <- setRefClass(    
  "modelUtils"
  
  , fields = list(
      commonFolder="character",
      projectDir="character",
      projectFile="character"
    )
  , methods = list(
    #
    #
    # Constructor
    #
    #
    initialize = function(commonFolder = configuration@commonFolder,
                          projectDir = "", 
                          projectFile = configuration@projectFile, ...)
    {
      
      callSuper(..., 
                commonFolder = commonFolder,
                projectDir = projectDir, 
                projectFile = projectFile)
    },
    #
    #
    # Copy all the xml and txt file from the source folder to the destiny folder.
    # It also create the destiny folder
    #
    #
    prepareProjectFolderAndFiles = function()
    {
      
      print("METHOD IN: copy_basefiles_")
      
      destFolder <- paste(commonFolder, "/", projectDir, sep="")
      
      mkdir(destFolder)
      
      statusPrjCnf  <- system(paste("cp -r ", commonFolder,"*.xml ", destFolder,sep=""))
      statusParFil  <- system(paste("cp -r ", commonFolder,"*.txt ", destFolder,sep=""))
      
      status1 <- 0
      
      if (statusParFil+statusPrjCnf+status1 != 0 )  stop("unable to copy basefiles") 
      
      print("METHOD OUT: copy_basefiles_")
      
      status <- statusParFil+statusPrjCnf+status1
      
      modifyProjectFile(destFolder)
      
      status
      
    },
    #
    #
    # Create a directory. If the directory exists it would be erased at first.
    #
    #    
    mkdir = function(destFolder)
    {
      print("METHOD IN: mkdir_")
      
      status  <- system(paste("rm -rf ", destFolder) )
      if (status != 0 )  stop("unable to delete old work directory") 
      status1 <- system(paste("mkdir -p ", destFolder))
      if (status1 != 0 )  stop("unable to create directory") 
      
      print("METHOD OUT: mkdir_")
      status+status1
      
      
    },
    #
    #
    # Add the working folder to the input and output in the project file
    #
    #
    modifyProjectFile = function(destFolder) {
      
      xmlFile <- paste(destFolder,projectFile, sep="")
      
      modifyXmlAttribute(xmlFile, "/ldndcproject/input/sources", "sourceprefix", projectDir)
      modifyXmlAttribute(xmlFile, "/ldndcproject/output/sinks", "sinkprefix", projectDir)
      
    },
    #
    #
    # Modify the xml file
    #
    #
    modifyXmlAttribute = function(xmlFile, node, attr, value) {
      xmlfile <- xmlTreeParse(xmlFile, useInternalNodes = TRUE)
      root <- xmlRoot(xmlfile, skip=FALSE)
      
      nodes <- getNodeSet(root, node)
      
      ## For each node, apply gsub on the content of the node
      xmlAttrs(nodes[[1]])[attr] = value
      
      saveXML(root, xmlFile, indent=TRUE)
      
      #Workaround
      restoreXMLSymbols(xmlFile)
    },
    #
    #
    # Get the attribute and its value from a xml file
    #
    #
    getXmlAttribute = function(xmlFile, xPath) {
      attr <- getNodeSet(xmlFile, xPath)
      attr
    },
    #
    #
    # Workaround method: We lose the < and > symbols in some attributes, so we replace them
    #
    #
    restoreXMLSymbols = function(xmlFile) {
      conn <- file(xmlFile)
      txt <- suppressWarnings(readLines(conn))
      txt <- gsub("&lt;", "<", txt)
      txt <- gsub("&gt;", ">", txt)
      txt <- append('<?xml version="1.0"?>',txt)
      writeLines(txt, conn)
      close(conn)
    }
    
    
    
    
  )#End methods List 
  
)#End RefClass





